/*=====================================================
Critical Links
Auto Configuration Server

Authors:
	Rui Eduardo Gouveia Gil - r-gil@critical-links.com

Date: 2010
Version 0.1
=======================================================*/
#include "httpd/HttpAuthentication.h"
#include "httpd/HttpACSInterface.h"
#include "httpd/HttpFileService.h"
#include "httpd/HttpFileUpload.h"
#include "httpd/HttpJavaScript.h"
#include "httpd/HttpServer.h"
#include "ACSConfig.h"

namespace acs
{

HttpServer::~HttpServer()
{
	LOG_INFO_STR("Deleting HttpServer...");

	if(this->webServer) delete this->webServer;

	LOG_INFO_STR("HttpServer deleted");
}

HttpServer::HttpServer() : webServer(NULL)
{
	LOG_INFO_STR("Creating HttpServer...");

	try
	{
		this->webServer = new pion::net::WebServer(ACSConfig::getPort());
		this->webServer->setMaxContentLength(256*1024*1024);
		this->webServer->setAuthentication(pion::net::HTTPAuthPtr(new HttpAuthentication()));
		this->webServer->addService("/JavaScriptInterface",new HttpJavaScript());
		this->webServer->addService("/ACSInterface",new HttpACSInterface());
		this->webServer->addService("/FileUpload",new HttpFileUpload());
		this->webServer->addService("",new HttpFileService());
		this->webServer->getSSLContext().use_certificate_file(ACSConfig::getCertificateFile(),boost::asio::ssl::context_base::pem);
		this->webServer->getSSLContext().use_rsa_private_key_file(ACSConfig::getPrivateKeyFile(),boost::asio::ssl::context_base::pem);
		this->webServer->setSSLFlag(true);
		this->webServer->start();
	}
	catch(std::exception& e)
	{
		Throw(rgcpp::RuntimeException(e.what()));
	}

	LOG_INFO_STR("HttpServer Created!");
}

}
